var log = console.log.bind(console);
$(function() {
	var people_len = 120;
	var arr = [];
	var re_arr = [];
	var html = '';
	var count3 = 0;
	var count2 = 0;
	var count1 = 0;
	var count0 = 0;
	var luck_num = 2;
	for(var i = 0; i < people_len; i++) {

		arr[i] = i + 1;
	}
	//	re_arr=[1,3,4,5,6,7,8,89,10,12,13,14,15,16]
	reduce(re_arr, arr);
	var share2 = (function() {
		var cInterval;

		return function(event) {
			var luck_html = '';

			if(cInterval) {
				log(typeof cInterval);
				clearInterval(cInterval);
				log("清除定时器");
			}
			//打乱数组顺序
			arr.sort(function() {
				return 0.5 - Math.random()
			})
			pop2(luck_num, arr)
			if(count2 > 2) {
				$(this).unbind("click");

			}

			if(count2 == 2) {

				alert("已抽完2次啦");
			} else {
				cInterval = setInterval(function() {

					for(var j = 1; j <= 10; j++) {
						$('.td' + j).text(parseInt(120 * Math.random()));

					}
				}, 100);
			}

			$('.stop_btn2').off('click').on('click', function(event) {
				event.preventDefault();
				//停止就+1;
				if(count2 == 2) {
					$(this).unbind("click");
					clearInterval(cInterval);
				} else {
					if(cInterval) {
						clearInterval(cInterval);
						//定义新数组
						count2++;
						var luck_arr = arr.slice(arr.length - 10);

						arr = arr.splice(0, arr.length - luck_arr.length);

						for(var j = 0; j < luck_arr.length; j++) {
							luck_html += '<span style="color:#fff">' + luck_arr[j] + ' </span>';

						}
						var shady_html = '';
						var shady_html2 = '';
						//shady幕布
						for(var j = 0; j < 5; j++) {
							shady_html += '<td>' + luck_arr[j] + ' </td>';
							shady_html2 += '<td>' + luck_arr[j + 5] + ' </td>';
						}
						for(var j = 1; j <= 10; j++) {
							$('.td' + j).text(luck_arr[j - 1]);
							//添加进本地
							var task = {
								id: 0,
								task_item:luck_arr[j - 1] ,
								add_time: new Date(),
								is_finished: false,
								price:'二等奖'
							};
							Tasks.Add(task);
							Tasks.AppendHtml(task);

						}

						log('luck.len ' + luck_arr.length);
						console.log("长度:" + arr.length + "   arr:" + arr);

						if(luck_html != '') {
							//测试代码
							$('.price3_result').append(luck_html + '<br>');
							luck_html = '';

							$('.shady_tip_table').append('<tr>' + shady_html + '</tr><tr>' + shady_html2 + '</tr>');

							$('.shady').fadeIn(1500);

							$('.shady_all2_table').append('<tr>' + shady_html + shady_html2 + '</tr>')
						}

					}
				}

			})
		}

	}());
	//替按钮绑定事件
	$('.start_btn2').on('click', share2);

	var share3 = (function() {
		var cInterval;

		return function(event) {
			var luck_html = '';

			if(cInterval) {

				clearInterval(cInterval);
				log("清除定时器");
			}
			//打乱数组顺序
			arr.sort(function() {
				return 0.5 - Math.random()
			})
			pop3(luck_num, arr);
			if(count3 > 2) {
				$(this).unbind("click");

			}

			if(count3 == 2) {

				alert("已抽完2次啦");
			} else {
				cInterval = setInterval(function() {

					for(var j = 1; j <= 25; j++) {
						$('.sp' + j).text(parseInt(120 * Math.random()));

					}
				}, 100);
			}

			$('.stop_btn3').off('click').on('click', function(event) {
				event.preventDefault();
				//停止就+1;
				if(count3 == 2) {
					$(this).unbind("click");
					clearInterval(cInterval);
				} else {
					if(cInterval) {
						clearInterval(cInterval);
						//定义新数组
						count3++;
						var luck_arr = arr.slice(arr.length - 25);

						arr = arr.splice(0, arr.length - luck_arr.length);

						for(var j = 0; j < luck_arr.length; j++) {
							luck_html += '<span style="color:#fff">' + luck_arr[j] + ' </span>';

						}
						var shady_html = '';
						var shady_html2 = '';
						//shady幕布
						for(var j = 0; j < 25; j += 5) {
							shady_html += '<tr ><td id="price3_shady_font">' + luck_arr[j] + ' </td><td id="price3_shady_font">' + luck_arr[j + 1] + ' </td><td id="price3_shady_font">' + luck_arr[j + 2] + ' </td><td id="price3_shady_font">' + luck_arr[j + 3] + ' </td><td id="price3_shady_font">' + luck_arr[j + 4] + ' </td></tr>';
							shady_html2 += '<tr ><td id="price3_shady_font2">' + luck_arr[j] + ' </td><td id="price3_shady_font2">' + luck_arr[j + 1] + ' </td><td id="price3_shady_font2">' + luck_arr[j + 2] + ' </td><td id="price3_shady_font2">' + luck_arr[j + 3] + ' </td><td id="price3_shady_font2">' + luck_arr[j + 4] + ' </td></tr>';
						}

						for(var j = 1; j <= 25; j++) {
							$('.sp' + j).text(luck_arr[j - 1]);
							var task = {
								id: 0,
								task_item:luck_arr[j - 1] ,
								add_time: new Date(),
								is_finished: false,
								price:'三等奖'
							};
							Tasks.Add(task);
							Tasks.AppendHtml(task);

						}

						log('luck.len ' + luck_arr.length);
						console.log("长度:" + arr.length + "   arr:" + arr);

						if(luck_html != '') {
							//测试代码
							$('.price3_result').append(luck_html + '<br>');
							luck_html = '';

							$('.shady_tip_table').append(shady_html);

							$('.shady').fadeIn(1500);
							
							$('.shady_all3_table').append(shady_html2)
						}

					}
				}

			})
		}

	}());

	$('.start_btn3').on('click', share3);

	var share1 = (function() {
		var cInterval;

		return function(event) {
			var luck_html = '';

			if(cInterval) {
				log(typeof cInterval);
				clearInterval(cInterval);
				log("清除定时器");
			}

			//打乱数组顺序

			arr.sort(function() {
				return 0.5 - Math.random();
			})
			if(count1 == 0) {
				haha(luck_num, arr);
			}

			//打乱数组顺序

			if(count1 > 2) {
				$(this).unbind("click");

			}

			if(count1 == 2) {

				alert("已抽完2次啦");
			} else {
				cInterval = setInterval(function() {

					for(var j = 1; j <= 5; j++) {
						$('.cp' + j).text(parseInt(120 * Math.random()));

					}
				}, 100);
			}

			$('.stop_btn1').off('click').on('click', function(event) {
				event.preventDefault();
				//停止就+1;
				if(count1 == 2) {
					$(this).unbind("click");
					clearInterval(cInterval);
				} else {
					if(cInterval) {
						clearInterval(cInterval);
						//定义新数组
						count1++;
						var luck_arr = arr.slice(arr.length - 5);

						arr = arr.splice(0, arr.length - luck_arr.length);

						for(var j = 0; j < luck_arr.length; j++) {
							luck_html += '<span style="color:#fff">' + luck_arr[j] + ' </span>';

						}
						var shady_html = '';
						var shady_html2 = '';
						//shady幕布
						for(var j = 0; j < 5; j++) {
							shady_html += '<td>' + luck_arr[j] + ' </td>';

						}
						for(var j = 1; j <= 5; j++) {
							$('.cp' + j).text(luck_arr[j - 1]);
							var task = {
								id: 0,
								task_item:luck_arr[j - 1] ,
								add_time: new Date(),
								is_finished: false,
								price:'一等奖'
							};
							Tasks.Add(task);
							Tasks.AppendHtml(task);
						}

						log('luck.len ' + luck_arr.length);
						console.log("长度:" + arr.length + "   arr:" + arr);

						if(luck_html != '') {
							//测试代码
							$('.price1_result').append(luck_html + '<br>');
							luck_html = '';

							$('.shady_tip_table').append('<tr>' + shady_html + '</tr>');

							$('.shady').fadeIn(1500);

							$('.shady_all1_table').append('<tr>' + shady_html + '</tr>')
						}

					}
				}

			})
		}

	}());
	$('.start_btn1').on('click', share1);

	var share0 = (function() {
		var cInterval;

		return function(event) {
			var luck_html = '';

			if(cInterval) {
				log(typeof cInterval);
				clearInterval(cInterval);
				log("清除定时器");
			}

			//打乱数组顺序

			arr.sort(function() {
				return 0.5 - Math.random();
			})
			//			if(count1==0){
			//				haha(luck_num, arr);
			//			}

			//打乱数组顺序

			if(count0 > 2) {
				$(this).unbind("click");

			}

			if(count0 == 2) {

				alert("已抽完2次啦");
			} else {
				cInterval = setInterval(function() {

					for(var j = 1; j <= 5; j++) {
						$('.tb' + j).text(parseInt(120 * Math.random()));

					}
				}, 100);
			}

			$('.stop_btn0').off('click').on('click', function(event) {
				event.preventDefault();
				//停止就+1;
				if(count0 == 2) {
					$(this).unbind("click");
					clearInterval(cInterval);
				} else {
					if(cInterval) {
						clearInterval(cInterval);
						//定义新数组
						count0++;
						var luck_arr = arr.slice(arr.length - 5);

						arr = arr.splice(0, arr.length - luck_arr.length);

						for(var j = 0; j < luck_arr.length; j++) {
							luck_html += '<span style="color:#fff">' + luck_arr[j] + ' </span>';

						}
						var shady_html = '';
						var shady_html2 = '';
						//shady幕布
						for(var j = 0; j < 5; j++) {
							shady_html += '<td>' + luck_arr[j] + ' </td>';

						}
						for(var j = 1; j <= 5; j++) {
							$('.cp' + j).text(luck_arr[j - 1]);
							var task = {
								id: 0,
								task_item:luck_arr[j - 1] ,
								add_time: new Date(),
								is_finished: false,
								price:'特等奖'
							};
							Tasks.Add(task);
							Tasks.AppendHtml(task);
						}

						log('luck.len ' + luck_arr.length);
						console.log("长度:" + arr.length + "   arr:" + arr);

						if(luck_html != '') {
							//测试代码
							$('.price1_result').append(luck_html + '<br>');
							luck_html = '';

							$('.shady_tip_table').append('<tr>' + shady_html + '</tr>');

							$('.shady').fadeIn(1500);

							$('.shady_all0_table').append('<tr>' + shady_html + '</tr>')
						}

					}
				}

			})
		}

	}());
	$('.start_btn0').on('click', share0);

	//本地记录
	var pp3=0;
	var pp2=0;
	var pp1=0;
	var Tasks = {
		show: function(obj) {
			obj.className = '';
			return this;
		},
		hide: function(obj) {
			obj.className = 'hide';
			return this;
		},
		//存储dom
		$taskItemList: $('#taskItemList'),
		//指针
		index: window.localStorage.getItem('Tasks:index'),
		//初始化
		init: function() {
			if(!Tasks.index) {
				window.localStorage.setItem('Tasks:index', Tasks.index = 0);
			}
			/*注册事件*/

			//初始化数据
			if(window.localStorage.length - 1) {
				var task_list = [];
				var key;
				for(var i = 0, len = window.localStorage.length; i < len; i++) {
					key = window.localStorage.key(i);
					console.log("key:" + key);
					if(/task:\d+/.test(key)) {
						task_list.push(JSON.parse(window.localStorage.getItem(key)));
					}
					console.log("task_list:" + task_list);
				}
				for(var i = 0, len = task_list.length; i < len; i++) {
					
					Tasks.AppendHtml(task_list[i]);
				}
			}
			//

			//

			$(".btn-info").click(function() {
				tableToExcel("taskItemList", '');
				console.log("下载");
			})
			$(".btn-danger").click(function() {

				var delete_data = confirm("是否清空所有数据");
				if(delete_data == true) {
					Tasks.clear();
					window.location.reload();
				}

			})
			//

		},
		//增加
		Add: function(task) {
			//更新指针
			window.localStorage.setItem('Tasks:index', ++Tasks.index);
			task.id = Tasks.index;
			window.localStorage.setItem("task:" + Tasks.index, JSON.stringify(task));
		},
		//修改
		Edit: function(task) {
			window.localStorage.setItem("task:" + task.id, JSON.stringify(task));
		},
		//删除
		Del: function(task) {
			window.localStorage.removeItem("task:" + task.id);
		},
		AppendHtml: function(task) {
//			
//			if(task.price=='三等奖'){
//				var html = '';
//				
//				
//				
//				html = '<td  class="taskItem" id="task_' + task.id + '" name="'+task.price+'">' + task.task_item + '</td>';
//			
//				$('.shady_all3_table').append(html);
//			}
//			
//			var html = '';
//			html = '<tr  class="taskItem" id="task_' + task.id + '">' + '<td>' + task.id + ' </td>' + task.task_item + '</tr>';
//
//			Tasks.$taskItemList.append(html);
		},
		clear: function(task) {
			window.localStorage.clear(task);
		}

	}

	Tasks.init();
})
//隐入幕布
$('.return_btn').click(function() {
	$('.shady').fadeOut('2');
	$('.shady_tip').css('display', 'block');
	$('.shady_tip_table').empty('tr');
});

$('.return2_btn').click(function() {
	$('.shady_tip_table').empty('tr');
	$('.shady').fadeOut('2');
	$('.shady_tip').css('display', 'block');
	$('.shady_tip2').addClass('none');
})
$('.look_luck_btn').click(function() {
	$('.shady_tip').css('display', 'none');
	if($('.shady_tip_span').text() == '特') {

		$('.shady_all0').removeClass('none');
	} else if($('.shady_tip_span').text() == '一') {

		$('.shady_all1').removeClass('none');
	} else if($('.shady_tip_span').text() == '二') {

		$('.shady_all2').removeClass('none');
	} else if($('.shady_tip_span').text() == '三') {

		$('.shady_all3').removeClass('none');
	}

})